package com.example.userserver.base;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.commons.entity.RoleDO;
import com.example.commons.entity.UserDO;
import com.example.commons.entity.UserRoleDO;
import com.example.commons.utils.ApiResult;
import com.example.commons.utils.EhCacheUtil;
import com.example.commons.utils.HttpContextUtils;
import com.example.commons.utils.IdCardUtil;
import com.example.userserver.service.UserRoleService;
import com.example.userserver.service.UserService;
import com.example.userserver.util.JWTUtil;
import com.example.userserver.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <pre>
 * </pre>
 *
 * <small> 2018年2月25日 | Aron</small>
 */
@Slf4j
public abstract class BaseController {

    @Autowired
    private UserService userService;

    private String RecoveryId="1375327865662226434";//产后恢复证书ID

    // 用户信息相关

    public UserDO getUser() {
        return ShiroUtils.getSysUser();
    }

    public Long getUserId() {
        return getUser().getId();
    }

    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }

    public String getUsername() {
        return getUser().getUsername();
    }

    public UserDO getClientUser(String token) {
        return userService.getByUserId( Long.valueOf(JWTUtil.getWxUserId(token)));

    }



    // 参数请求、分页

    /**
     * <pre>
     * 自动获取分页参数，返回分页对象page
     * </pre>
     *
     * <small> 2018年4月15日 | Aron</small>
     *
     * @param e
     * @return
     */
    public <E> Page<E> getPage(Class<E> e) {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        Page<E> page = new Page<>(pageNumber, pageSize);
        //支持sort、order参数
        HttpServletRequest request = getRequest();
        String sort = request.getParameter("sort");
        if (StringUtils.isNotBlank(sort)) {
            page.setOrderByField(sort);
            String order = request.getParameter("order");
            if (StringUtils.isNotBlank(order)) {
                page.setAsc("asc".equalsIgnoreCase(order));
            }
        }
        return page;
    }


    /**
     * <pre>
     * 自动获取分页参数，返回分页对象page
     * </pre>
     *
     * <small> 2018年4月15日 | Aron</small>
     *
     * @param e
     * @return
     */
    public <E> Page<E> getPages(Class<E> e) {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 100);
        Page<E> page = new Page<>(pageNumber, pageSize);
        //支持sort、order参数
        HttpServletRequest request = getRequest();
        String sort = request.getParameter("sort");
        if (StringUtils.isNotBlank(sort)) {
            page.setOrderByField(sort);
            String order = request.getParameter("order");
            if (StringUtils.isNotBlank(order)) {
                page.setAsc("asc".equalsIgnoreCase(order));
            }
        }
        return page;
    }


    private int getParaToInt(String key, int defaultValue) {
        String pageNumber = HttpContextUtils.getHttpServletRequest().getParameter(key);
        if (StringUtils.isBlank(pageNumber)) {
            return defaultValue;
        }
        return Integer.parseInt(pageNumber);
    }

    // Servlet

    protected HttpServletRequest getRequest() {
        return HttpContextUtils.getHttpServletRequest();
    }

    protected HttpServletResponse getResponse() {
        return HttpContextUtils.getHttpServletResponse();
    }

    // 响应结果

    protected ApiResult<String> fail() {
        return ApiResult.fail();
    }

    protected ApiResult<String> success() {
        return success(null);
    }

    protected <T> ApiResult<T> success(T t) {
        return ApiResult.success(t);
    }

//    public String getRoleNames() {
//        String roleName = "";
//        List<UserRoleDO> userRole = userRoleService.findByKv("userId", this.getUserId());
//        if (userRole != null && userRole.size() > 0) {
//            for (UserRoleDO roleDO : userRole) {
//                RoleDO role = roleService.selectById(roleDO.getRoleId());
//                roleName = roleName + role.getRoleName() + ",";
//            }
//        }
//        return roleName;
//    }
//
//    public String getRoleNamesToken(Long userId) {
//        String roleName = "";
//        List<UserRoleDO> userRole = userRoleService.findByKv("userId", userId);
//        if (userRole != null && userRole.size() > 0) {
//            for (UserRoleDO roleDO : userRole) {
//                RoleDO role = roleService.selectById(roleDO.getRoleId());
//                roleName = roleName + role.getRoleName() + ",";
//            }
//        }
//        return roleName;
//    }



    /**
     * 对考生技能考试进行分组
     *
     * @Description: 对考生技能考试进行分组，参数：调用类型（0生成试卷时，1考生分配时），试卷模板id，考试批次id
     * @Param: [type, tptemplateId, ebatchId]
     * @return: boolean
     * @Author: kl
     * @Date: 2020-06-06
     */


//    private int maxDurationNum() {
//        int maxDurationNum = 20;
//        Map mapstus = new HashMap();
//        mapstus.clear();
//        mapstus.put("name", "技能考试每组最大时长");
//        mapstus.put("type", "max_duration");
//        String maxDuration = dictService.selDicBy(mapstus);
//        if (org.apache.commons.lang.StringUtils.isNotBlank(maxDuration)) {
//            //判断是否为数字
//            if (IdCardUtil.isNumeric(maxDuration)) {
//                maxDurationNum = Integer.parseInt(maxDuration);
//            }
//        }
//        return maxDurationNum;
//    }


    //组合证书编号

    //组合证书编号
    public String comNumber1(String cerNumEnd, String grade, String province, Integer numPeriods, String yearRe, String trainingMath,String cprofileId) {
        if(grade.equals("健康财富规划公益师资")){
            return "XHWPTC20220326";
        }
        if(cprofileId.equals(RecoveryId)){//产后恢复
            if (grade.equals("高级师资")) {
                cerNumEnd = cerNumEnd + "11";
            } else if (grade.equals("考评员")) {
                cerNumEnd = cerNumEnd + "41";
            } else if (grade.equals("高级考评员")) {
                cerNumEnd = cerNumEnd + "31";
            }
            else if (grade.equals("考务技术员")) {
                cerNumEnd = cerNumEnd + "51";
            }else if(grade.equals("考务技术员-结业证书")) {
                cerNumEnd = cerNumEnd + "61";
            }else if (grade.equals("督导员")) {
                cerNumEnd = cerNumEnd + "71";
            }else if(grade.equals("督导员-结业证书")) {
                cerNumEnd = cerNumEnd + "81";
            }else if(grade.equals("种子师资")) {
                cerNumEnd = cerNumEnd + "91";
            }else if(grade.equals("公益师资")) {
                cerNumEnd = cerNumEnd + "13";
            }else if(grade.equals("优秀学员")) {
                cerNumEnd = cerNumEnd + "23";
            }else if(grade.equals("优秀培训教师")) {
                cerNumEnd = cerNumEnd + "33";
            }else if(grade.equals("优秀考务工作者")) {
                cerNumEnd = cerNumEnd + "43";
            }else if(grade.equals("特殊贡献奖")) {
                cerNumEnd = cerNumEnd + "53";
            }
            else {
                cerNumEnd = cerNumEnd + "21";
            }
        }else{//幼儿照护
            if (grade.equals("高级师资")) {
                cerNumEnd = cerNumEnd + "10";
            } else if (grade.equals("考评员")) {
                cerNumEnd = cerNumEnd + "40";
            } else if (grade.equals("高级考评员")) {
                cerNumEnd = cerNumEnd + "30";
            }
            else if (grade.equals("考务技术员")) {
                cerNumEnd = cerNumEnd + "50";
            }else if(grade.equals("考务技术员-结业证书")) {
                cerNumEnd = cerNumEnd + "60";
            }else if (grade.equals("督导员")) {
                cerNumEnd = cerNumEnd + "70";
            }else if(grade.equals("督导员-结业证书")) {
                cerNumEnd = cerNumEnd + "80";
            }else if(grade.equals("种子师资")) {
                cerNumEnd = cerNumEnd + "90";
            }else if(grade.equals("公益师资")) {
                cerNumEnd = cerNumEnd + "12";
            }else if(grade.equals("综合公益师资")) {
                cerNumEnd = cerNumEnd + "55";
            }else if(grade.equals("优秀学员")) {
                cerNumEnd = cerNumEnd + "22";
            }else if(grade.equals("优秀培训教师")) {
                cerNumEnd = cerNumEnd + "32";
            }else if(grade.equals("优秀考务工作者")) {
                cerNumEnd = cerNumEnd + "42";
            }else if(grade.equals("特殊贡献奖")) {
                cerNumEnd = cerNumEnd + "52";
            }
            else {
                cerNumEnd = cerNumEnd + "20";
            }
        }


        //省份
        String proCode = selProvinceCode(province);
        if (proCode.contains("错误")) {
            return "开班区域(省份)填写错误，请按照证书省份对照表填写！";
        }
        //cerNumEnd = cerNumEnd + proCode + yearRe + trainingMath + WithZoreUtil.frontCompWithZore(numPeriods, 2);

        return cerNumEnd;
    }

    public String selProvinceCode(String province) {
        if (province.equals("北京市")) {
            return "11";
        } else if (province.equals("天津市")) {
            return "12";
        } else if (province.equals("河北省")) {
            return "13";
        } else if (province.equals("山西省")) {
            return "14";
        } else if (province.equals("内蒙古")) {
            return "15";
        } else if (province.equals("辽宁省")) {
            return "21";
        } else if (province.equals("吉林省")) {
            return "22";
        } else if (province.equals("黑龙江")) {
            return "23";
        } else if (province.equals("上海市")) {
            return "31";
        } else if (province.equals("江苏省")) {
            return "32";
        } else if (province.equals("浙江省")) {
            return "33";
        } else if (province.equals("安徽省")) {
            return "34";
        } else if (province.equals("福建省")) {
            return "35";
        } else if (province.equals("江西省")) {
            return "36";
        } else if (province.equals("山东省")) {
            return "37";
        } else if (province.equals("河南省")) {
            return "41";
        } else if (province.equals("湖北省")) {
            return "42";
        } else if (province.equals("湖南省")) {
            return "43";
        } else if (province.equals("广东省")) {
            return "44";
        } else if (province.equals("广西")) {
            return "45";
        } else if (province.equals("海南省")) {
            return "46";
        } else if (province.equals("重庆市")) {
            return "50";
        } else if (province.equals("四川省")) {
            return "51";
        } else if (province.equals("贵州省")) {
            return "52";
        } else if (province.equals("云南省")) {
            return "53";
        } else if (province.equals("西藏")) {
            return "54";
        } else if (province.equals("陕西省")) {
            return "61";
        } else if (province.equals("甘肃省")) {
            return "62";
        } else if (province.equals("青海省")) {
            return "63";
        } else if (province.equals("宁夏")) {
            return "64";
        } else if (province.equals("新疆")) {
            return "65";
        } else if (province.equals("新疆兵团")) {
            return "66";
        } else if (province.equals("台湾")) {
            return "71";
        } else if (province.equals("香港")) {
            return "81";
        } else if (province.equals("澳门")) {
            return "82";
        } else if (province.equals("海外")) {
            return "91";
        } else {
            return "错误";
        }
    }

    public void delProvinEhcahe(String cType) {
        if (cType.equals("0")) {
            EhCacheUtil.put("cerNumberTra北京市", null);
            EhCacheUtil.put("cerNumberTra天津市", null);
            EhCacheUtil.put("cerNumberTra河北省", null);
            EhCacheUtil.put("cerNumberTra山西省", null);
            EhCacheUtil.put("cerNumberTra内蒙古", null);
            EhCacheUtil.put("cerNumberTra辽宁省", null);
            EhCacheUtil.put("cerNumberTra吉林省", null);
            EhCacheUtil.put("cerNumberTra黑龙江", null);
            EhCacheUtil.put("cerNumberTra上海市", null);
            EhCacheUtil.put("cerNumberTra江苏省", null);
            EhCacheUtil.put("cerNumberTra浙江省", null);
            EhCacheUtil.put("cerNumberTra安徽省", null);
            EhCacheUtil.put("cerNumberTra福建省", null);
            EhCacheUtil.put("cerNumberTra江西省", null);
            EhCacheUtil.put("cerNumberTra山东省", null);
            EhCacheUtil.put("cerNumberTra河南省", null);
            EhCacheUtil.put("cerNumberTra湖北省", null);
            EhCacheUtil.put("cerNumberTra湖南省", null);
            EhCacheUtil.put("cerNumberTra广东省", null);
            EhCacheUtil.put("cerNumberTra广西", null);
            EhCacheUtil.put("cerNumberTra海南省", null);
            EhCacheUtil.put("cerNumberTra重庆市", null);
            EhCacheUtil.put("cerNumberTra四川省", null);
            EhCacheUtil.put("cerNumberTra贵州省", null);
            EhCacheUtil.put("cerNumberTra云南省", null);
            EhCacheUtil.put("cerNumberTra西藏", null);
            EhCacheUtil.put("cerNumberTra陕西省", null);
            EhCacheUtil.put("cerNumberTra甘肃省", null);
            EhCacheUtil.put("cerNumberTra青海省", null);
            EhCacheUtil.put("cerNumberTra宁夏", null);
            EhCacheUtil.put("cerNumberTra新疆", null);
            EhCacheUtil.put("cerNumberTra新疆兵团", null);
            EhCacheUtil.put("cerNumberTra台湾", null);
            EhCacheUtil.put("cerNumberTra香港", null);
            EhCacheUtil.put("cerNumberTra澳门", null);
            EhCacheUtil.put("cerNumberTra海外", null);
        } else {
            EhCacheUtil.put("cerNumberExa北京市", null);
            EhCacheUtil.put("cerNumberExa天津市", null);
            EhCacheUtil.put("cerNumberExa河北省", null);
            EhCacheUtil.put("cerNumberExa山西省", null);
            EhCacheUtil.put("cerNumberExa内蒙古", null);
            EhCacheUtil.put("cerNumberExa辽宁省", null);
            EhCacheUtil.put("cerNumberExa吉林省", null);
            EhCacheUtil.put("cerNumberExa黑龙江", null);
            EhCacheUtil.put("cerNumberExa上海市", null);
            EhCacheUtil.put("cerNumberExa江苏省", null);
            EhCacheUtil.put("cerNumberExa浙江省", null);
            EhCacheUtil.put("cerNumberExa安徽省", null);
            EhCacheUtil.put("cerNumberExa福建省", null);
            EhCacheUtil.put("cerNumberExa江西省", null);
            EhCacheUtil.put("cerNumberExa山东省", null);
            EhCacheUtil.put("cerNumberExa河南省", null);
            EhCacheUtil.put("cerNumberExa湖北省", null);
            EhCacheUtil.put("cerNumberExa湖南省", null);
            EhCacheUtil.put("cerNumberExa广东省", null);
            EhCacheUtil.put("cerNumberExa广西", null);
            EhCacheUtil.put("cerNumberExa海南省", null);
            EhCacheUtil.put("cerNumberExa重庆市", null);
            EhCacheUtil.put("cerNumberExa四川省", null);
            EhCacheUtil.put("cerNumberExa贵州省", null);
            EhCacheUtil.put("cerNumberExa云南省", null);
            EhCacheUtil.put("cerNumberExa西藏", null);
            EhCacheUtil.put("cerNumberExa陕西省", null);
            EhCacheUtil.put("cerNumberExa甘肃省", null);
            EhCacheUtil.put("cerNumberExa青海省", null);
            EhCacheUtil.put("cerNumberExa宁夏", null);
            EhCacheUtil.put("cerNumberExa新疆", null);
            EhCacheUtil.put("cerNumberExa新疆兵团", null);
            EhCacheUtil.put("cerNumberExa台湾", null);
            EhCacheUtil.put("cerNumberExa香港", null);
            EhCacheUtil.put("cerNumberExa澳门", null);
            EhCacheUtil.put("cerNumberExa海外", null);
        }
    }

}